﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using System.Windows.Forms;

namespace deneme
{
    class ClassFillCity
    {
         SqlConnection con = new SqlConnection(ClassSqlConnection.ConnectionString);
         public string gidilecekyol ="";
         public string mesafe;
         private int iCityCounter = 0;
         private double dToplamMesafe = 0;

        public void sehirekle(string kalkis,string varis)
        {
            string strQuery = null;
            string sehir1 = null;
            string sehir2 = null;
            string[] sehirler = new string[74];
            Dictionary<string, Node> _dictNodes = new Dictionary<string, Node>();
            if (con.State == ConnectionState.Closed) con.Open();

            for (int i = 1; i < 75; i++)
            {
                sehir1 = "SELECT SehirAd FROM tblSehir WHERE ID = " + i + " ";
                SqlCommand sqcom1 = new SqlCommand(sehir1, con);
                sehirler[i - 1] = sqcom1.ExecuteScalar().ToString();
                _dictNodes.Add(sehirler[i - 1], new Node(sehirler[i - 1]));
            }

            List<Node> _nodes = new List<Node>();
            foreach (Node n in _dictNodes.Values)
            {
                _nodes.Add(n);
            }

            List<Edge> _edges = new List<Edge>();
            for (int i = 1; i < 75; i++)
            {

                for (int j = 1; j < 75; j++)
                {
                    strQuery = "SELECT  [" + j + "]  FROM tblMesafe WHERE ID = " + i + " ";

                    SqlCommand sqcom = new SqlCommand(strQuery, con);

                    if (sqcom.ExecuteScalar().ToString() != "")
                    {

                        //Sehir1
                        sehir1 = "SELECT SehirAd FROM tblSehir WHERE ID = " + i + " ";
                        SqlCommand sqcom1 = new SqlCommand(sehir1, con);
                        sehir1 = sqcom1.ExecuteScalar().ToString();

                        //Sehir2
                        sehir2 = "SELECT SehirAd FROM tblSehir WHERE ID = " + j + " ";
                        SqlCommand sqcom2 = new SqlCommand(sehir2, con);
                        sehir2 = sqcom2.ExecuteScalar().ToString();
                        //DugumEkle(sehir1, sehir2, Convert.ToInt32(sqcom.ExecuteScalar().ToString()));

                        _edges.Add(new Edge(_dictNodes[sehir1], _dictNodes[sehir2], Convert.ToInt32(sqcom.ExecuteScalar().ToString())));
                    }
                }
            }
            Dijkstra d = new Dijkstra(_edges, _nodes);
            d.calculateDistance(_dictNodes[kalkis]);
            List<Node> path = d.getPathTo(_dictNodes[varis]);
            if (path.Count > 0)
            {
               // gidilecekyol[] = "";
                foreach (Node n in path)
                {
                    gidilecekyol = gidilecekyol + n.Name + "->";
                    iCityCounter++;
                }
            }
            for (int i = 0; i < iCityCounter - 1; i++)
            {
                dToplamMesafe = dToplamMesafe + d.getDistanceBetween(path[i], path[i + 1]);
            }

            mesafe = dToplamMesafe.ToString();
            iCityCounter = 0;
            dToplamMesafe = 0;

        }
            


            
        }
}
